package com.neu.flashmall.user.controller;

import com.neu.common.utils.Result;
import com.neu.flashmall.user.entity.UmsAddress;
import com.neu.flashmall.user.service.UmsAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/ums_address")
@Api(value = "ums地址管理Controller")
public class UmsAddressController {
    @Resource
    private UmsAddressService umsAddressService;

    @GetMapping("/getPaged")
    @ApiOperation(value = "地址管理获得分页信息")
    public Result getPaged(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "3") Integer pageSize){
        return Result.ok().put(umsAddressService.getPaged(pageNum,pageSize));
    }

    @GetMapping("/changeDefaultStatus")
    @ApiOperation(value = "修改默认地址信息")
    public Result changeDefaultStatus(@Param(value = "addressId") Long addressId){
        if(umsAddressService.changeDefaultStatus(addressId) > 0){
            return Result.ok();
        }
        return Result.error();
    }

    @PostMapping("/insert")
    @ApiOperation(value = "添加地址信息")
    public Result insert(@RequestBody UmsAddress umsAddress){
        //insert返回userId
        UmsAddress address = umsAddressService.insert(umsAddress);
        if(address.getId() > 0){
            return Result.ok().put(address).setMessage("地址添加成功");
        }
        return Result.error().setMessage("地址添加失败");
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新地址信息")
    public Result update(@RequestBody UmsAddress umsAddress){
        if(umsAddressService.update(umsAddress) > 0){
            return Result.ok().setMessage("地址修改成功");
        }
        return Result.error().setMessage("地址修改失败");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除地址信息")
    public Result delete(Long addressId){
        String result = umsAddressService.delete(addressId);
        if(result.equals("地址删除成功")){
            return Result.ok().setMessage(result);
        }
        return Result.ok().setMessage(result);
    }

    @GetMapping("/getAddressById")
    @ApiOperation(value = "获取地址信息")
    public Result getAddressById(Long id){
        UmsAddress umsAddress = umsAddressService.getOneById(id);
        return Result.ok().put(umsAddress);
    }

    @GetMapping("/getDefaultAddressByUserId")
    @ApiOperation(value = "获取默认地址信息")
    public Result getDefaultAddressByUserId(Long id){
        UmsAddress umsAddress = umsAddressService.getDefaultAddressByUserId(id);
        return Result.ok().put(umsAddress);
    }

}
